package com.ag.oa.core.controller;

import com.ag.oa.core.entity.vo.NewsVO;
import com.ag.oa.core.service.INewsService;
import com.ag.oa.util.CustomUtil;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class AdminController {

    @Autowired
    private INewsService newsService;

    private static final Logger logger = LoggerFactory.getLogger(AdminController.class);

    /**
     * 动态文章分页
     */
    @GetMapping("/trends")
    public String getNewsForPageList(Map<String, Object> map, Integer page) {

        List<NewsVO> newsList = newsService.getForNewsDataList(null, page, 20);
        map.put("newsList", newsList);
        return "trends";
    }

    /**
     * ajax 分页获取动态文章数据
     */
    @PostMapping("/trends")
    public @ResponseBody String getNewsForPageList(Integer page, Integer size) {

        JSONObject res = new JSONObject();
        boolean success = true;
        try {
            page = page==null? 1: (page - 1);
            List<NewsVO> newsList = newsService.getForNewsDataList(null, page, size);
            int totalRecord = newsService.getTotalRecordsCount();
            res.put("newsList", newsList);
            res.put("totalRecord", totalRecord);
        } catch (Exception e) {
            success = false;
            e.printStackTrace();
        }
        res.put("success", success);
        return res.toJSONString();
    }

    /**
     * 跳转 oa 动态文章的添加
     */
    @GetMapping("/trends/add")
    public String gotoAddNewsPage() {

        return "trends_add";
    }

    /**
     * 保存 oa 动态文章的添加/修改
     */
    @RequestMapping(value = "/trends/save", method = RequestMethod.POST)
    public @ResponseBody String saveAddTrends2News(Integer id, String title, String contentURL, String coverURL
            , String content, Integer queue, HttpSession session) {

        logger.info(session.getAttribute("userName") +"--添加动态文章, id: "+ id +", title: "+ title
                +", contentURL: "+ contentURL +", coverURL: "+ coverURL +", content: "+ content +", queue: "+ queue);

        String res = "success";
        try {
            newsService.saveNewsByAdmin(id, title, contentURL, coverURL, content, queue);
        } catch (Exception e) {
            e.printStackTrace();
            res = "fail";
        }
        return res;
    }

    /**
     * 删除动态文章
     */
    @RequestMapping(value = "/trends/saveDel", method = RequestMethod.POST)
    public @ResponseBody String delNews(Integer newsId, HttpSession session) {

        logger.info(session.getAttribute("userName") +"--删除动态文章, id: "+ newsId);

        String res = "success";
        try {
            newsService.delNewsById(newsId);
        } catch (Exception e) {
            e.printStackTrace();
            res = "fail";
        }
        return res;
    }

    /**
     * 跳转编辑
     */
    @GetMapping("/trends/edit/{id}")
    public String gotoEditNewsPage(@PathVariable("id") Integer id, Map<String, Object> map) {

        int source = 1;
        NewsVO news = newsService.getNewsInfoById(id);
        if (news!=null && CustomUtil.strNotEmpty(news.getContentUrl())) {
            source = 0;
        }
        map.put("newsInfo", news);
        map.put("source", source);
        return "trends_edit";
    }

    @PostMapping("/trends/stickyArticle")
    @ResponseBody
    public String stickyArticle(Integer id) {
        String res = "success";
        try {
            newsService.updateSticky(1, id);
        } catch (Exception e) {
            e.printStackTrace();
            res = "fail";
        }
        return res;
    }

    @PostMapping("/trends/unStickyArticle")
    @ResponseBody
    public String unStickyArticle(Integer id) {
        String res = "success";
        try {
            newsService.updateSticky(0, id);
        } catch (Exception e) {
            e.printStackTrace();
            res = "fail";
        }
        return res;
    }


}
